<template>
  <div>
    <el-card class="search-card">
      <basic-table-form
        ref="formData"
        isScalingBtn
        :formColumns="formItemList"
        :dataSource="formData"
        :defaultExpandStatus="false"
        :handleSearchForm="handleQueryForm"
        @getFormData="getFormData"
      >
      </basic-table-form>
    </el-card>
    <el-card class="table-card">
      <basic-table
        showActionCol
        :loading="tableInfo.loading"
        :columns="tableInfo.columns"
        :dataSource="tableInfo.list"
        :pagination="pagination"
        :pageSizes="pageSizes"
        :tableHeight="442"
        :showSelectCol="false"
        :actionColWidth="120"
        :actionRender="renderTableActionFn"
        @pageChange="handlePageChange"
        @pageSizeChange="handlePageSizeChange"
      >
      </basic-table>
    </el-card>
    <batch-maintain-dialog :visible.sync="dialogVisble"></batch-maintain-dialog>
  </div>
</template>
<script>
import { mapGetters } from 'vuex';
import BasicTableForm from '@/components/BasicForm';
import BasicTable from '@/components/BasicTable';
import batchMaintainDialog from '@/components/common/dialog/batchMaintainDialog';
import { FORM_ITEM_LIST, TABLE_COLUMNS } from './constants';
import { getBtnsId } from '@/libs/btnsPermission';
import basePage from '@/mixins/commodity/basePage';
import { getCommodityListService } from '@/api/commodity/commodityAccessManage/commodityAdd';
import { handleOpenNewPage } from '@/utils/bizUtils';

export default {
  mixins: [basePage],
  components: {
    BasicTableForm,
    BasicTable,
    batchMaintainDialog
  },
  data () {
    return {
      dialogVisble: false,
      formItemList: FORM_ITEM_LIST,
      tableInfo: {
        list: [],
        selection: [],
        columns: TABLE_COLUMNS,
        loading: false
      },
      formData: {
        recordNo: '',
        operName: '',
        operDeptName: '',
        createOpeTime: '',
        createOpeTime2: '',
      },
    };
  },
  computed: {
    ...mapGetters('permissions/systemBtns', ['showBtn']),
  },
  created () {
    this.handleQueryForm();
  },
  methods: {
    // 获取表格数据
    async getTableData () {
      const data = {
        ...this.formData,
        ...this.pagination,
        commitTypes: ['ADDSKU']
      };
      const curBtnId = getBtnsId('查询');
      sessionStorage.setItem('buttonId', curBtnId);
      const retData = await getCommodityListService(
        data,
        state => { this.tableInfo.loading = state; }
      );
      if (retData) {
        this.tableInfo.list = retData.results;
        this.pagination.totalNum = retData.totalNum;
      }
    },
    // 表格操作按钮
    renderTableActionFn (h, { row }) {
      const permissionBtns = [
        { label: '查看单据', methodFn: this.handleViewRecord },
      ];
      return (<div class="table-action">
        {permissionBtns.map(item => {
          const btn = <el-button
            type="text"
            onClick={() => item.methodFn(row)}
          >{item.label}</el-button>
          if (!item.hasAuth) { return btn; } // 没有权限的无需判断权限
          if (this.showBtn(item.label)) { return btn; }
        })}
      </div>);
    },
    // 查看单据
    handleViewRecord (row) {
      handleOpenNewPage(this, {
        name: 'viewSkuRecord',
        query: {
          recordNo: row.recordNo
        }
      });
    },
  }
};
</script>
